package com.qq.weixin.mp.aes;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author li.jiang 2016.01.28 代码review并修正潜在问题
 *
 */
@SuppressWarnings("serial")
public class AesException extends Exception {

    public static final int OK = 0;
    public static final int VALIDATE_SIGNATURE_ERROR = -40001;
    public static final int PARSE_XML_ERROR = -40002;
    public static final int COMPUTE_SIGNATURE_ERROR = -40003;
    public static final int ILLEGAL_AES_KEY = -40004;
    public static final int VALIDATE_APPID_ERROR = -40005;
    public static final int ENCRYPT_AES_ERROR = -40006;
    public static final int DECRYPT_AES_ERROR = -40007;
    public static final int ILLEGAL_BUFFER = -40008;
    /**
     * 编码和名称的映射
     */
    private static final Map<Integer, String> CODE_NAME_MAP = new LinkedHashMap<Integer, String>(8);

    // 映射数据初始化
    static {
        CODE_NAME_MAP.put(VALIDATE_SIGNATURE_ERROR, "签名验证错误");
        CODE_NAME_MAP.put(PARSE_XML_ERROR, "xml解析失败");
        CODE_NAME_MAP.put(COMPUTE_SIGNATURE_ERROR, "sha加密生成签名失败");
        CODE_NAME_MAP.put(ILLEGAL_AES_KEY, "SymmetricKey非法");
        CODE_NAME_MAP.put(VALIDATE_APPID_ERROR, "appid校验失败");
        CODE_NAME_MAP.put(ENCRYPT_AES_ERROR, "aes加密失败");
        CODE_NAME_MAP.put(DECRYPT_AES_ERROR, "aes解密失败");
        CODE_NAME_MAP.put(ILLEGAL_BUFFER, "解密后得到的buffer非法");
    }
    private final int code;

    public AesException(int code) {
        super(getMessage(code));
        this.code = code;
    }

    private static String getMessage(int code) {
        return CODE_NAME_MAP.get(code);
    }

    public int getCode() {
        return code;
    }
}
